# Container for building System76 Open Firmware

# NOTE: The repository is specified in the image name to make it explicit
# which source is being trusted to provide the image.
ARG CONTAINER_IMAGE="docker.io/library/debian:bookworm-20250520-slim"

ARG COREBOOT_REPO="https://github.com/coreboot/coreboot.git"
ARG COREBOOT_COMMIT="25.03"

ARG SDCC_REPO="https://svn.code.sf.net/p/sdcc/code"
ARG SDCC_REV="14648"
ARG SDCC_VERSION="4.4.0"

ARG RUST_TOOLCHAIN="1.85.0"

# Build coreboot toolchains
FROM ${CONTAINER_IMAGE} as crossgcc-build
ARG COREBOOT_COMMIT
ARG COREBOOT_REPO
WORKDIR /tmp

RUN apt-get --quiet update \
    && apt-get --quiet install --no-install-recommends --assume-yes \
        bash \
        bison \
        bzip2 \
        ca-certificates \
        curl \
        flex \
        g++ \
        gcc \
        git \
        gnat \
        libssl-dev \
        m4 \
        make \
        patch \
        pkgconf \
        python-is-python3 \
        python3 \
        tar \
        xz-utils \
        zlib1g-dev \
    && apt-get clean

RUN git clone ${COREBOOT_REPO} \
    && cd coreboot \
    && git checkout ${COREBOOT_COMMIT}

RUN make -C coreboot \
        CPUS=$(nproc) BUILD_LANGUAGES=ada,c,c++ DEST=/opt/xgcc \
        crossgcc-i386 crossgcc-x64 \
    && rm -rf coreboot

# Build SDCC toolchain
FROM ${CONTAINER_IMAGE} as sdcc-build
ARG SDCC_REPO
ARG SDCC_REV
ARG SDCC_VERSION
WORKDIR /tmp

RUN apt-get --quiet update \
    && apt-get --quiet install --no-install-recommends --assume-yes \
        autoconf \
        automake \
        bison \
        ca-certificates \
        flex \
        g++ \
        gcc \
        libboost-dev \
        make \
        subversion \
        zlib1g-dev \
    && apt-get clean

RUN svn checkout \
    --depth infinity \
    --revision ${SDCC_REV} \
    ${SDCC_REPO}/tags/sdcc-${SDCC_VERSION}/sdcc \
    sdcc

# Only the MCS-51 port is needed.
RUN cd sdcc \
    && sh ./configure \
        --disable-z80-port \
        --disable-z180-port \
        --disable-r2k-port \
        --disable-r2ka-port \
        --disable-r3ka-port \
        --disable-sm83-port \
        --disable-tlcs90-port \
        --disable-ez80_z80-port \
        --disable-z80n-port \
        --disable-ds390-port \
        --disable-ds400-port \
        --disable-pic14-port \
        --disable-pic16-port \
        --disable-hc08-port \
        --disable-s08-port \
        --disable-stm8-port \
        --disable-pdk13-port \
        --disable-pdk14-port \
        --disable-pdk15-port \
        --disable-mos6502-port \
        --disable-ucsim \
        --disable-sdcdb \
        --disable-non-free \
        --prefix= \
    && make -j $(nproc) \
    && make install DESTDIR=/opt/sdcc

# Set up environment for building firmware-open
FROM ${CONTAINER_IMAGE}
ARG RUST_TOOLCHAIN
COPY --from=crossgcc-build /opt/xgcc /opt/xgcc
COPY --from=sdcc-build /opt/sdcc /opt/sdcc
ENV COREBOOT_COMMIT "${COREBOOT_COMMIT}"
ENV XGCCPATH "/opt/xgcc/bin"
ENV SDCC_PATH "/opt/sdcc"
ENV SDCC_REV "${SDCC_REV}"
ENV SDCC_VERSION "${SDCC_VERSION}"
ENV CARGO_HOME "/root/.cargo"
ENV PATH "$XGCCPATH:$SDCC_PATH/bin:$CARGO_HOME/bin:$PATH"

RUN apt-get --quiet update \
    && apt-get --quiet install --no-install-recommends --assume-yes \
        bash \
        binutils \
        ca-certificates \
        ccache \
        cmake \
        curl \
        dosfstools \
        g++ \
        gcc \
        git \
        git-lfs \
        libc6-dev \
        libhidapi-dev \
        libnss3-dev \
        libssl-dev \
        libudev-dev \
        make \
        mtools \
        parted \
        pkgconf \
        python-is-python3 \
        python3 \
        shellcheck \
        uncrustify \
        uuid-dev \
        xxd \
    && apt-get clean

# TODO: Use distro rustup package when available (Trixie).
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
    | sh -s -- -y --no-modify-path --default-toolchain stable

# XXX: rustup 1.27 does not recognize toolchain if preceding option specifies
# a comma separated list as an argument with a space.
# Ref: https://github.com/rust-lang/rustup/issues/4073
RUN rustup toolchain install \
        --no-self-update \
        --target x86_64-unknown-linux-gnu,x86_64-unknown-uefi \
        --profile minimal \
        --component=clippy,rustfmt \
        ${RUST_TOOLCHAIN}

WORKDIR /workspace
CMD ["bash"]
